package roborally.model;

import java.util.*;
import roborally.model.interfaces.*;
/**
 * @Extends Item
 * @Implements	Terminatable
 *
 */
public class SurpriseBox extends Item implements Terminatable{
	
	/**
	 * Constructor for a SurpriseBox object
	 * @param weight
	 * @effect
	 * 			|super(weight)
	 */
	public SurpriseBox(int weight) {
		super(weight);
	}
	
	/**
	 * Default constructor for a SurpriseBox object
	 * @effect
	 * 			|this(Item.WEIGHT_DEFAULT) 
	 */
	public SurpriseBox(){
		this(Item.WEIGHT_DEFAULT);
	}
	
	/**
	 * The surprising effect a surprisebox can have when used on a robot, which is given as parameter
	 * @param 	robot
	 * 			The robot on which the surprisebox is used
	 * @effect	|if(new Random().nextInt(3) == 0) then robot.hit()
	 * @effect	|if(new Random().nextInt(3) == 1) then robot.getField().getBoard().teleportElement(robot)
	 * @effect	|if(new Random().nextInt(3) == 2) then
	 * 			|	int rand = new Random().nextInt(3)
	 * 			|	if(rand == 0) then robot.addItem(new Battery())
	 * 			|	if(rand == 1) then robot.addItem(new RepairKit())
	 * 			|	if(rand == 2) then robot.addItem(new SurpriseBox())
	 * @post	|new.isTerminated() == true
	 */
	public void useOn(Robot robot) {
		int rand = new Random().nextInt(3);
		switch(rand){
			case 0: { //The surprisebox explodes and the robot is hit
				robot.hit(); break;
			}
			case 1: { //The robot is teleported
				robot.getField().getBoard().teleportElement(robot); break;
			}
			case 2: { //Other content
				rand = new Random().nextInt(3);
				Item item;
				switch(rand){
					case 0: item = new Battery(); break;
					case 1: item = new RepairKit(); break;
					case 2: item = new SurpriseBox(); break;
					default: item = new Battery(); //Added so the item object will always be initialized (otherwise compile error)
				}
				robot.addItem(item);
			}
		}
		terminate();//we terminate the surprise box for it is used.
	}
	
	/**
	 * A method that represents the effect a surprisebox experiences when it is hit by the laser of a robot
	 * @post	|for(Item item: getField().getBoard().getItemsOnNeighbourFields(getField()))
	 * 			|	item.isTerminated() == true
	 */
	@Override
	public void hit() {
		Set<Item> items = getField().getBoard().getItemsOnNeighbourFields(getField());
		for(Item item: items)
			item.terminate();
		terminate();
	}
}
